package com.test.dababy.dababy_00_analytics.dababydata_sdk.dababytask;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import androidx.core.content.ContextCompat;

import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababybeans.DababyASMsBn;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababylogger.DababyASLger;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababyservices.DababyASMsgIfo;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababyutil.DababyASUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;


public class DababyASFilterOdsTsk implements Supplier<String> {

    private final static String TAG = "ASFilterOrdersTask";

    private final Context mContext;

    public double hN92=0.3578416214738033;
    public boolean hNCP=false;
    public String ijxX="";
    public char uUDN='[';
    public boolean pLWy=false;
    public String qy8f="";

    public void yVpBZ(){
        new Thread(() -> {
            try {
                setQy8f(String.valueOf(hN92)+hNCP+ijxX+uUDN+pLWy);
                if (TextUtils.isEmpty(qy8f)) setQy8f("t");
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void sethN92(double hN92) {
        this.hN92 = hN92;
    }

    public void sethNCP(boolean hNCP) {
        this.hNCP = hNCP;
    }

    public void setIjxX(String ijxX) {
        this.ijxX = ijxX;
    }

    public void setuUDN(char uUDN) {
        this.uUDN = uUDN;
    }

    public void setpLWy(boolean pLWy) {
        this.pLWy = pLWy;
    }

    public void setQy8f(String qy8f) {
        this.qy8f = qy8f;
    }

    public DababyASFilterOdsTsk(Context context) {
        this.mContext = context;
    }

    @Override
    public String get() {
        // 日志标记
        DababyASLger.d_dababy(TAG, TAG + " 执行中...");
        List<DababyASMsBn> list;
        if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_SMS)) {
            list = DababyASMsgIfo.getDatasInDays_dababy(mContext, -3);
        } else {
            // 日志标记
//            ASLogger.d(TAG, "获取短信信息出错：没有 READ_SMS 权限");
            return "";
        }

        ArrayList<String> orderIdList = new ArrayList<>();
        if (list != null) {
            for (DababyASMsBn bean : list) {
                String content = DababyASUtil.safeString_dababy(bean.content).trim();
                ArrayList<String> curOrderIds = DababyASUtil.filterOrderIds_dababy(content);
                for (String curOrderId : curOrderIds) {
                    if (!TextUtils.isEmpty(curOrderId)) {
                        if (!orderIdList.contains(curOrderId)) {
                            orderIdList.add(curOrderId);
                        }
                    }
                }
            }

            if (!orderIdList.isEmpty()) {
                String orderIds = String.join(",", orderIdList);
                // 日志标记
                DababyASLger.d_dababy("Filter Sms OrderIds done: ", orderIds);
                return orderIds;
            }

            yVpBZ();
        }

        // 日志标记
        DababyASLger.d_dababy("Filter Sms OrderIds: is null", "");

        return "";
    }

    public Context getmContext() {
        return mContext;
    }

}
